package im.yisme.model.fileio;

import im.yisme.model.Lyrics;
import im.yisme.model.LyricsRepository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.util.Log;

public class LyricsRepositoryReader {
	
	private String path;
	
	public LyricsRepositoryReader(){
		path = DirManager.getRepositoryDir();
	}
	
	public void read(LyricsRepository lyricsRepository) throws IOException{
		ArrayList<Lyrics> list = new ArrayList<Lyrics>();
		lyricsRepository.setLyricsList(list);
		
		File file = new File(path + "lyrics.repo");
		if (!(file.exists() && file.isFile())) return;
		
		BufferedReader reader = new BufferedReader(new FileReader(file));	
		String info;
		while((info = reader.readLine()) != null){
			try {
				String[] datas = info.split("&");
				int source = datas.length >= 4 ? Integer.parseInt(datas[3]) : Lyrics.ORIGINAL;
				Lyrics lyrics = new Lyrics(Long.parseLong(datas[0]), datas[1], datas[2], null, source);
				lyrics.setScore(Integer.parseInt(datas[4]));
				lyrics.setNoteNum(Integer.parseInt(datas[5]));
				lyrics.setTotalTime(datas[6]);
				
				LyricsReader lyricsReader = new LyricsReader();
				lyricsReader.read(lyrics);
				
				lyrics.setNoteNum(lyrics.getLyricsActions().size());
				if(lyrics.getTotalTime() == null) lyrics.setTotalTime("3:50");
				
				list.add(lyrics);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				continue;
			}
		}
		
		Log.i("Lyricssadklfjasldf",list.size()+"");
		reader.close();
	}
}
